using System;
using System.Collections.Generic;
using System.Text;
using OpenALDotNet;
using OpenALDotNet.Streams;

namespace Barrage.Audios
{
    /// <summary>
    /// StreamAudio uses an OGG audiostream as source instead of an audiofile.
    /// </summary>
    public class StreamAudio:Audio
    {
        const int BUFFER_SIZE = 1024*32;
        const int BUFFER_COUNT = 8;
        AudioSource source;
        AudioStreamPlayer player;
        bool playing = false;

        /// <summary>
        /// Constructor. Loads the OGG file specified
        /// </summary>
        /// <param name="file">path to an OGG audio file</param>
        public StreamAudio(string file)
        {
            source = new AudioSource();
            player=new AudioStreamPlayer(new VorbisAudioStream(file),source,BUFFER_SIZE,BUFFER_COUNT);
            //source.EnqueueBuffer(file.Buffer);
            // Non spatialized sound
            source.IsRelative = false;
            // Do not loop by default
            source.IsLooping = false;
            
        }

        /// <summary>
        /// \internal Updates sound's buffers
        /// </summary>
        /// <param name="item">Item playing this sound</param>
        public override void Update(Item item){
            player.Update();
        }

        /// <summary>
        /// \internal Gets / Sets whether the sound must loop when finished or not.
        /// </summary>
        public override bool Loop
        {
            get
            {
                return source.IsLooping;
            }
            set
            {
                source.IsLooping = value;
            }
        }

        /// <summary>
        /// \internal Gets / Sets the sound's speed. Default is 1.
        /// </summary>
        public override float Speed
        {
            get
            {
                return source.Pitch;
            }
            set
            {
                source.Pitch = value;
            }
        }

        /// <summary>
        /// \internal Starts the sound
        /// </summary>
        public override void Start()
        {
            player.Play();
            playing = true;
        }

        /// <summary>
        /// \internal Stops the sound
        /// </summary>
        public override void Stop()
        {
            player.Stop();
            playing = false;
        }

        /// <summary>
        /// \internal Returns true when the sound is being played
        /// </summary>
        public override bool Playing
        {
            get { return playing; }
        }

        /// <summary>
        /// \internal Gets / Sets the sound volume
        /// </summary>
        public override float Volume
        {
            get
            {
                return source.Gain;
            }
            set
            {
                source.Gain = value;
            }
        }

        /// <summary>
        /// \internal Clear resources
        /// </summary>
        public override void Dispose()
        {
            player.Dispose();
            source.Dispose();
            base.Dispose();
        }

    }
}
